package com.cardmanager.pro;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.log4j.PropertyConfigurator;
//import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import cn.jh.common.exception.ApiExceptionHandler;
import cn.jh.common.interceptor.AuthenticationHeaderInterceptor;

@SpringBootApplication
@EnableDiscoveryClient
@EnableCaching
public class CreditCardManagerStartUp extends WebMvcConfigurerAdapter {

	public static void main(String[] args) throws Exception {
        if (args.length == 0) args = new String[] { "--spring.profiles.active=dev" };
        
        String schedule_task_on_off = "false";
		String port = "8023";
		String logPath = "logs/CreditCardManager.log";
//		if (args.length > 1) {
//			port = args[1];
//			if (args.length > 2) {
//				logPath = args[2];
//			} else {
//				logPath = "logs/CreditCardManager.log";
//			}
//		} else {
//			port = "8023";
//		}
		for (String arg : args) {
			if ("activeScan".equalsIgnoreCase(arg)) {
				schedule_task_on_off = "true";
			}
			

			if (arg.contains("port=")) {
				port = arg.substring(arg.indexOf("=")+1,arg.length()).trim();
			}
			
			if (arg.contains("logPath=")) {
				logPath = arg.substring(arg.indexOf("=")+1,arg.length()).trim();
			}
		}
		Map<String,Object> propertiesMap = new HashMap<>();
		propertiesMap.put("server.port", port);
		propertiesMap.put("schedule-task.on-off", schedule_task_on_off);
		new SpringApplicationBuilder(CreditCardManagerStartUp.class).properties(propertiesMap).run(args);
		Properties logProp = new Properties();
		logProp.load(CreditCardManagerStartUp.class.getClassLoader().getResourceAsStream("log4j.properties"));
		logProp.setProperty("log4j.appender.file.file", logPath);
		PropertyConfigurator.configure(logProp);
//		SpringApplication.run(CreditCardManagerStartUp.class, args);
	}

	@Autowired
	private Environment env;

	@Bean
	public DataSource getDataSource() throws Exception {
		String dataBaseName = env.getProperty("mysql.dataBaseName");
		Properties props = new Properties();
		props.put("driverClassName", env.getProperty("spring.datasource.driver-class-name"));
		props.put("url", env.getProperty("spring.datasource.url")+"/"+dataBaseName+"?useUnicode=true&characterEncoding=UTF-8");
		props.put("username", env.getProperty("spring.datasource.username"));
		props.put("password", env.getProperty("spring.datasource.password"));
		return DruidDataSourceFactory.createDataSource(props);
	}

	/*
	 * @Bean public DataSource
	 * druidDataSource(@Value("{spring.datasource.driver-class-name}") String
	 * driver,
	 * 
	 * @Value("{spring.datasource.url}") String url,
	 * 
	 * @Value("{spring.datasource.username}") String username,
	 * 
	 * @Value("{spring.datasource.password}") String password){
	 * 
	 * DruidDataSource druidDataSource = new DruidDataSource();
	 * 
	 * druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url);
	 * druidDataSource.setUsername(username);
	 * druidDataSource.setPassword(password);
	 * 
	 * try{ druidDataSource.setFilters("stat, wall"); } catch(SQLException e){
	 * e.printStackTrace(); }
	 * 
	 * return druidDataSource; }
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new AuthenticationHeaderInterceptor()).addPathPatterns("/**");
	}

	@Bean
	public ApiExceptionHandler getApiExceptionHander() {
		return new ApiExceptionHandler();
	}

}
